package com.polaris.live.common.adapter

import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.loadmore.BaseLoadMoreView
import com.chad.library.adapter.base.loadmore.LoadMoreStatus
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.util.getItemView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.common.R
import com.polaris.live.common.constant.PagConst.LIVE_LOADING_PAG
import org.libpag.PAGImageView

/**
 * BaseAdapter
 *
 * @author Created by lucas on 2023/12/25 02:01
 */
abstract class BaseAdapter<T, VH : BaseViewHolder>(
    @LayoutRes private val layoutResId: Int,
    data: MutableList<T>? = null
) : BaseQuickAdapter<T, VH>(layoutResId, data), LoadMoreModule

class AdapterLoadMoreView : BaseLoadMoreView() {

    override fun getRootView(parent: ViewGroup): View =
        parent.getItemView(R.layout.widget_adapter_loading)

    override fun getLoadingView(holder: BaseViewHolder): View =
        holder.getView(R.id.load_more_loading_view)

    override fun getLoadComplete(holder: BaseViewHolder): View =
        holder.getView(R.id.load_more_load_complete_view)

    override fun getLoadEndView(holder: BaseViewHolder): View =
        holder.getView(R.id.load_more_load_end_view)

    override fun getLoadFailView(holder: BaseViewHolder): View =
        holder.getView(R.id.load_more_load_fail_view)

    override fun convert(holder: BaseViewHolder, position: Int, loadMoreStatus: LoadMoreStatus) {
        super.convert(holder, position, loadMoreStatus)

        if (loadMoreStatus == LoadMoreStatus.Loading) {
            holder.getView<PAGImageView>(R.id.pag_view).apply {
                path = LIVE_LOADING_PAG
                setRepeatCount(-1)
                play()
            }
        } else {
            holder.getView<PAGImageView>(R.id.pag_view).pause()
        }
    }
}